﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PlayDeezGames.Common;
using System.Xml.Linq;
using System.Collections.Generic;
using DungeonDelver.Engine.Names;

namespace DungeonDelver.Engine
{
    public class Configuration : DescriptoredEntityBase
    {
        public class Factory : IEntityFactory
        {
            public IEntity LoadFromXElement(IEntity theParent, XElement theXElement)
            {
                return new Configuration(theParent as Game, theXElement);
            }

            public IEntity Create(IEntity theParent)
            {
                return new Configuration(theParent as Game);
            }

            private Factory() { }

            public static void Register()
            {
                EntityFactories.RegisterFactory(Names.EngineFactories.ConfigurationFactory, new Configuration.Factory());
            }
            public static void Unregister()
            {
                EntityFactories.UnregisterFactory(Names.EngineFactories.ConfigurationFactory);
            }

        }

        public Game Game
        {
            get
            {
                return ParentEntity as Game;
            }
        }

        private Configuration(Game parent)
            : base(parent)
        {
            this[Names.EngineTables.Items] = EntityFactories.Create(this,Names.EngineFactories.ConfigurationTableFactory) as ConfigurationTable;
            this[Names.EngineTables.Creatures] = EntityFactories.Create(this, Names.EngineFactories.ConfigurationTableFactory) as ConfigurationTable;
            this[Names.EngineTables.Terrains] = EntityFactories.Create(this, Names.EngineFactories.ConfigurationTableFactory) as ConfigurationTable;
            this[Names.EngineTables.PropertyGroups] = EntityFactories.Create(this, Names.EngineFactories.ConfigurationTableFactory) as ConfigurationTable;
            this[Names.EngineTables.Messages] = EntityFactories.Create(this, Names.EngineFactories.ConfigurationTableFactory) as ConfigurationTable;
        }

        private Configuration(Game parent, XElement theXElement)
            : base(parent, theXElement)
        {
        }

        private Dictionary<string, ConfigurationTable> configurationTables = new Dictionary<string, ConfigurationTable>();

        public Dictionary<string, ConfigurationTable>.KeyCollection Tables
        {
            get
            {
                return configurationTables.Keys;
            }
        }

        public ConfigurationTable this[string index]
        {
            get
            {
                if (configurationTables.ContainsKey(index))
                {
                    return configurationTables[index];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (configurationTables.ContainsKey(index))
                {
                    configurationTables[index].ParentEntity = null;
                    configurationTables.Remove(index);
                }
                if (value != null)
                {
                    configurationTables[index] = value;
                }
            }
        }


        protected override System.Xml.Linq.XElement OnToXElement()
        {
            XElement result = new XElement(Names.EngineXNames.Configuration);
            result.Add(new XAttribute(PlayDeezGames.Common.Names.CommonXNames.Factory, Names.EngineFactories.ConfigurationFactory));
            return result;
        }

        protected override void OnFromXElement(System.Xml.Linq.XElement theXElement)
        {
            base.OnFromXElement(theXElement);
        }

        public static Configuration Create(Game theParent)
        {
            return EntityFactories.Create(theParent, EngineFactories.ConfigurationFactory) as Configuration;
        }
        public static Configuration Load(Game theParent, XElement theXElement)
        {
            return EntityFactories.Load(theParent, theXElement) as Configuration;
        }
    }

}